//
//  UserSubmitQuestionCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/30.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "UserSubmitQuestionCell.h"
#import "ShowPhotoView.h"

@interface UserSubmitQuestionCell ()

@property (weak, nonatomic) IBOutlet UIImageView *userIconImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet UILabel *listenCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *speechImageView;

@property (weak, nonatomic) IBOutlet UIView *replyView;
@property (weak, nonatomic) IBOutlet UILabel *watingReplyView;
@property (weak, nonatomic) IBOutlet UILabel *replyDescribe;

@property (weak, nonatomic) IBOutlet ShowPhotoView *photoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoTop;


@end

@implementation UserSubmitQuestionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.userIconImageView.layer.cornerRadius = 16;
    self.userIconImageView.clipsToBounds = YES;
    self.userIconImageView.layer.borderWidth = 0.5f;
    self.userIconImageView.layer.borderColor = kContentColor.CGColor;
    
    self.nameLabel.textColor = kSubitleColor;
    self.dateLabel.textColor = kContentColor;
    self.contentLabel.textColor = kSubitleColor;
    self.listenCountLabel.textColor = kContentColor;
    self.replyDescribe.text = MYLocalizedString(@"ASKED_REPLY", nil);
    self.replyView.userInteractionEnabled = YES;
    [self.replyView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(replyTapAction)]];
    
    NSMutableArray *imageArray = [NSMutableArray array];
    
    for (int i = 1; i <= 3; i ++) {
        
        UIImage *image = [UIImage imageNamed:[@"speech_" stringByAppendingString:@(i).stringValue]];
        
        [imageArray addObject:image];
    }
    
    self.speechImageView.animationImages = imageArray;
    self.speechImageView.animationDuration = 1.0;
    self.speechImageView.animationRepeatCount = 0;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setQuestion:(Question *)question {
    //    1禁用，2未回复，3已回复，4拒绝
    _question = question;
    
    [self.userIconImageView setImageURL:[NSURL URLWithString:question.touristFace] placeholderImage:[UIImage imageNamed:@"default"]];
    self.nameLabel.text = question.touristName;
    self.contentLabel.text = question.question;
    self.timeLabel.text = [question.audiotime stringByAppendingString:@"s"];
    self.listenCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"LISTEN_COUNT", nil),question.seeTimes];
    
    
    NSString *dateStr = question.modifytime ? question.modifytime : question.createtime;
    
    NSInteger length = 10;
    
    if (dateStr.length < 10) {
        length = dateStr.length;
    }
    
    self.dateLabel.text = [dateStr substringWithRange:NSMakeRange(0, length)];
    
    if (question.pictureArray.count > 0) {
        
        self.photoHeight.constant = 76;
        self.photoTop.constant = 5;
        self.photoView.dataSource = question.pictureArray;
        self.photoView.superVC = self.superVC;
        self.photoView.hidden = NO;
        
    } else {
        NSLog(@"------- 没有图片");
        
        self.photoHeight.constant = 0;
        self.photoTop.constant = 0;
        self.photoView.hidden = YES;
    }
    
    
    if ([question.status integerValue] == 3) {
        
        self.watingReplyView.hidden = YES;
        self.replyView.hidden = NO;
        self.listenCountLabel.hidden = NO;
    } else {
        
        self.watingReplyView.hidden = NO;
        self.replyView.hidden = YES;
        self.listenCountLabel.hidden = YES;
        
        if (question.status.integerValue == 2) {
            self.watingReplyView.text =  MYLocalizedString(@"MYQUESTION_WAIT_REPLY", nil);
            self.watingReplyView.textColor = kContentColor;
        } else {
            
            self.watingReplyView.text = MYLocalizedString(@"MYQUESTION_REFUSE_DESCRIBE", nil);
            self.watingReplyView.textColor = kThemeRedColor;
        }
    }
}

- (void)startPlay {
    
    [self.speechImageView startAnimating];
}

- (void)stopPlay {
    
    [self.speechImageView stopAnimating];
}

- (void)replyTapAction {
    
    [_delegate answerAudioTapped:self question:self.question];
}

@end
